package com.fr.ticket;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;


/**
 * @author 付荣
 * @version 创建时间：2019年4月16日 上午11:01:24
 */
public class CtripTicketAndGoodsDetail {

	private static final String simple_file = "E:\\ticket\\ctrip_simple.txt";
	private static final String detail_file = "E:\\ticket\\ctrip_detail.txt";

	private static final String tab = "\t";
	private static final String line = "\n";

	public static void main(String[] args) throws IOException {

		TransportClient client = ESUtils.getClient();
		SearchRequestBuilder builder = client.prepareSearch("product_20190419").setTypes("product_20190419").setSize(1000);
		builder.setScroll(new TimeValue(10000000));
		// QueryBuilder queryBuilder =
		// QueryBuilders.rangeQuery("price").gte(0.1);
		// builder.setQuery(queryBuilder);
		FileWriter simpleWriter = new FileWriter(simple_file);
		FileWriter detailWriter = new FileWriter(detail_file);
		SearchResponse searchResponse = builder.get();
		Map<String, Integer> countMap = new HashMap<>();
		Map<String, Integer> countPriceMap = new HashMap<>();
		Map<String, Long> countgoodsMap = new HashMap<>();
		Set<String> set = new HashSet<>();
		while (true) {
			for (SearchHit hit : searchResponse.getHits().getHits()) {
				Map<String, Object> source = hit.getSource();
				StringBuffer simple = new StringBuffer();

				String id = source.get("id").toString();
				String name = source.get("name").toString();
				name = name.replaceAll("\r", "").replaceAll("\t", "").replaceAll("\n", "");
				// String vslat = source.get("vslat").toString();
				// String vslon = source.get("vslon").toString();
				String cityname = source.get("cityname").toString();
				String star = source.get("star").toString();
				String price = source.get("price").toString();
				String address;
				Object addr = source.get("address");
				if (addr == null) {
					address = "";
				} else {
					address = addr.toString().replaceAll("\r", "").replaceAll("\t", "").replaceAll("\n", "");
				}
				String provinceName = DestStaticData.map.get(cityname);
				if (provinceName == null) {
					set.add(cityname);
				}
				simple.append(provinceName).append(tab).append(id).append(tab).append(name).append(tab).append(address).append(tab).append(star).append(tab).append(price);
				simpleWriter.append(simple).append(line);
				Integer count = countMap.get(provinceName);
				if (count == null) {
					count = 0;
				}
				count++;
				countMap.put(provinceName, count);
				try {
					double p = Double.parseDouble(price);
					if (p > 0) {
						Integer countPrice = countPriceMap.get(provinceName);
						if (countPrice == null) {
							countPrice = 0;
						}
						countPrice++;
						countPriceMap.put(provinceName, countPrice);
						SearchRequestBuilder goodsSearchBuilder = client.prepareSearch("goods_20190419").setTypes("goods_20190419").setSize(1000);
						goodsSearchBuilder.setQuery(QueryBuilders.termQuery("productId", id));
						SearchResponse sr2 = goodsSearchBuilder.get();
						boolean first = true;
						for (SearchHit sh : sr2.getHits().getHits()) {
							StringBuffer detail = new StringBuffer();
							Map<String, Object> goodsSource = sh.getSource();
							String goodsId = goodsSource.get("id").toString();
							String goodsName = goodsSource.get("name").toString();
							goodsName = goodsName.replaceAll("\r", "").replaceAll("\t", "").replaceAll("\n", "");
							String goodsprice = String.valueOf(goodsSource.get("price"));
							Object rainfos = goodsSource.get("rainfos");
							List<Map<String, Object>> ctripDescList = (List<Map<String, Object>>)rainfos ;
							StringBuffer yuding = new StringBuffer();
							StringBuffer shiyong = new StringBuffer();
							StringBuffer tuigai = new StringBuffer();
							StringBuffer feiyong = new StringBuffer();

							if (CollectionUtils.isNotEmpty(ctripDescList)) {
								for (Map<String, Object> o : ctripDescList) {
									String title = o.get("title").toString();
									String subtitle = o.get("subtitle").toString();
									List<Map<String,String>> desclist = (List<Map<String,String>>)o.get("desclist");
									if (CollectionUtils.isNotEmpty(desclist)) {
										if ("预订说明".equals(title)) {
											yuding.append(subtitle).append(":").append(getDesc(desclist)).append(";");
										} else if ("费用说明".equals(title)) {
											feiyong.append(subtitle).append(":").append(getDesc(desclist)).append(";");
										} else if ("退改说明".equals(title)) {
											tuigai.append(subtitle).append(":").append(getDesc(desclist)).append(";");
										} else if ("使用说明".equals(title)) {
											shiyong.append(subtitle).append(":").append(getDesc(desclist)).append(";");
										}
									}
								}
							}
							if (first) {
								detail.append(simple).append(tab);
								first = false;
							} else {
								detail.append(tab).append(tab).append(tab).append(tab).append(tab).append(tab);
							}
							detail.append(goodsId).append(tab).append(goodsName).append(tab).append(goodsprice).append(tab).append(yuding).append(tab).append(shiyong).append(tab).append(feiyong)
									.append(tab).append(tuigai).append(tab).append(line);
							detailWriter.append(detail);
						}
						Long goodsCount = countgoodsMap.get(provinceName);
						if (goodsCount == null) {
							goodsCount = 0L;
						}
						goodsCount += sr2.getHits().getTotalHits();
						countgoodsMap.put(provinceName, goodsCount);
					}else{
						detailWriter.append(simple).append(line);
					}
				} catch (Exception e) {
					System.err.println(e);
				}
			}
			searchResponse = client.prepareSearchScroll(searchResponse.getScrollId()).setScroll(new TimeValue(10000000)).get();
			if (searchResponse.getHits().getHits().length == 0) {
				break;
			}
		}
		System.out.println(countPriceMap);
		System.out.println(countMap);
		System.out.println(countgoodsMap);
		System.out.println(set);

		simpleWriter.close();
		detailWriter.close();
	}

	private static String getDesc(List<Map<String,String>> desclist) {
		StringBuffer sb = new StringBuffer();
		for (Map<String,String> o : desclist) {
			sb.append(o.get("desc").replaceAll("\r", "").replaceAll("\t", "").replaceAll("\n", "")).append(",");
		}
		return sb.toString();
	}
}
